package com.wu.foodsafetyplayer.utils;

/**
 * 跳转权限设置页面
 */

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;

public class PermissionManagement {
    private static final String TAG = PermissionManagement.class.getSimpleName();
    private static final String MANUFACTURER_HUAWEI = "huawei";//华为
    private static final String MANUFACTURER_MEIZU = "meizu";//魅族
    private static final String MANUFACTURER_XIAOMI = "xiaomi";//小米
    private static final String MANUFACTURER_SONY = "sony";//索尼
    private static final String MANUFACTURER_OPPO = "oppo";
    private static final String MANUFACTURER_LG = "lg";
    private static final String MANUFACTURER_VIVO = "vivo";
    private static final String MANUFACTURER_SAMSUNG = "samsung";//三星
    private static final String MANUFACTURER_LETV = "letv";//乐视
    private static final String MANUFACTURER_ZTE = "zte";//中兴
    private static final String MANUFACTURER_YULONG = "yulong";//酷派
    private static final String MANUFACTURER_LENOVO = "lenovo";//联想
    private static final String MANUFACTURER_360 = "360";//联想

    private static boolean isAppSettingOpen = false;

    /**
     * 此函数可以自己定义
     *
     * @param activity
     */
    public static void GoToSetting(Activity activity, int requstCode) {
        Log.e(TAG, Build.MANUFACTURER + "-------" + Build.MANUFACTURER.toLowerCase());
        switch (Build.MANUFACTURER.toLowerCase()) {
            case MANUFACTURER_HUAWEI:
                Log.e(TAG, "GoToSetting: 进来了华为---------");
                Huawei(activity, requstCode);
                break;
            case MANUFACTURER_MEIZU:
                Meizu(activity, requstCode);
                break;
            case MANUFACTURER_XIAOMI:
                Xiaomi(activity, requstCode);
                break;
            case MANUFACTURER_SONY:
                Sony(activity, requstCode);
                break;
            case MANUFACTURER_OPPO:
                OPPO(activity, requstCode);
                break;
            case MANUFACTURER_LG:
                LG(activity, requstCode);
                break;
            case MANUFACTURER_LETV:
                Letv(activity, requstCode);
                break;
            case MANUFACTURER_360:
                _360(activity, requstCode);
                break;
            case MANUFACTURER_VIVO:
                Vivo(activity, requstCode);
                break;
            default:
                activity.startActivityForResult(getAppDetailSettingIntent(activity), requstCode);
                break;
        }
    }

    /**
     * 华为跳转权限设置页
     *
     * @param activity
     */
    public static void Huawei(Activity activity, int requstCode) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("packageName", activity.getPackageName());
            ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");
            intent.setComponent(comp);
            activity.startActivityForResult(intent, requstCode);
            isAppSettingOpen = false;
        } catch (Exception e) {
            openAppDetailSetting(activity, requstCode);
        }
    }

    /**
     * 魅族跳转权限设置页，测试时，点击无反应，具体原因不明
     *
     * @param activity
     */
    public static void Meizu(Activity activity, int requstCode) {
        try {
            Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.putExtra("packageName", activity.getPackageName());
            activity.startActivityForResult(intent, requstCode);
            isAppSettingOpen = false;
        } catch (Exception e) {
            openAppDetailSetting(activity, requstCode);
        }
    }

    /**
     * 小米，功能正常
     *
     * @param activity
     */
    public static void Xiaomi(Activity activity, int requstCode) {
        try { // MIUI 8 9
            Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
            localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
            localIntent.putExtra("extra_pkgname", activity.getPackageName());
            activity.startActivityForResult(localIntent, requstCode);
            isAppSettingOpen = false;
        } catch (Exception e) {
            try { // MIUI 5/6/7
                Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
                localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
                localIntent.putExtra("extra_pkgname", activity.getPackageName());
                activity.startActivityForResult(localIntent, requstCode);
                isAppSettingOpen = false;
            } catch (Exception e1) { // 否则跳转到应用详情
                openAppDetailSetting(activity, requstCode);
            }
        }
    }

    /**
     * 索尼，6.0以上的手机非常少，基本没看见
     *
     * @param activity
     */
    public static void Sony(Activity activity, int requstCode) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("packageName", activity.getPackageName());
            ComponentName comp = new ComponentName("com.sonymobile.cta", "com.sonymobile.cta.SomcCTAMainActivity");
            intent.setComponent(comp);
            activity.startActivityForResult(intent, requstCode);
            isAppSettingOpen = false;
        } catch (Exception e) {
            openAppDetailSetting(activity, requstCode);
        }
    }

    /**
     * @param context 跳转至权限页面
     */
    private static void Vivo(Activity context, int requstCode) {
        Intent localIntent;
        try {
            if (((Build.MODEL.contains("Y85")) && (!Build.MODEL.contains("Y85A"))) || (Build.MODEL.contains("vivo Y53L"))) {
                localIntent = new Intent();
                localIntent.setClassName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.PurviewTabActivity");
                localIntent.putExtra("packagename", context.getPackageName());
                localIntent.putExtra("tabId", "1");
                context.startActivityForResult(localIntent, requstCode);
            } else {
                localIntent = new Intent();
                localIntent.setClassName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.SoftPermissionDetailActivity");
                localIntent.setAction("secure.intent.action.softPermissionDetail");
                localIntent.putExtra("packagename", context.getPackageName());
                context.startActivityForResult(localIntent, requstCode);
            }
        } catch (Exception e) {
            openAppDetailSetting(context, requstCode);
        }
    }

    /**
     * OPPO
     *
     * @param activity
     */
    public static void OPPO(Activity activity, int requstCode) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("packageName", activity.getPackageName());
            ComponentName comp = new ComponentName("com.color.safecenter", "com.color.safecenter.permission.PermissionManagerActivity");
            intent.setComponent(comp);
            activity.startActivityForResult(intent, requstCode);
            isAppSettingOpen = false;
        } catch (Exception e) {
            openAppDetailSetting(activity, requstCode);
        }
    }

    /**
     * LG经过测试，正常使用
     *
     * @param activity
     */
    public static void LG(Activity activity, int requstCode) {
        try {
            Intent intent = new Intent("android.intent.action.MAIN");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("packageName", activity.getPackageName());
            ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.Settings$AccessLockSummaryActivity");
            intent.setComponent(comp);
            activity.startActivityForResult(intent, requstCode);
            isAppSettingOpen = false;
        } catch (Exception e) {
            openAppDetailSetting(activity, requstCode);
        }
    }

    /**
     * 乐视6.0以上很少，基本都可以忽略了，现在乐视手机不多
     *
     * @param activity
     */
    public static void Letv(Activity activity, int requstCode) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("packageName", activity.getPackageName());
            ComponentName comp = new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.PermissionAndApps");
            intent.setComponent(comp);
            activity.startActivityForResult(intent, requstCode);
            isAppSettingOpen = false;
        } catch (Exception e) {
            openAppDetailSetting(activity, requstCode);
        }
    }

    /**
     * 只能打开到自带安全软件
     *
     * @param activity
     */
    public static void _360(Activity activity, int requstCode) {
        try {
            Intent intent = new Intent("android.intent.action.MAIN");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("packageName", activity.getPackageName());
            ComponentName comp = new ComponentName("com.qihoo360.mobilesafe", "com.qihoo360.mobilesafe.ui.index.AppEnterActivity");
            intent.setComponent(comp);
            activity.startActivityForResult(intent, requstCode);
        } catch (Exception e) {
            openAppDetailSetting(activity, requstCode);
        }
    }

    /**
     * 系统设置界面
     *
     * @param activity
     */
    public static void SystemConfig(Activity activity, int requstCode) {
        Intent intent = new Intent(Settings.ACTION_SETTINGS);
        activity.startActivityForResult(intent, requstCode);
    }

    /**
     * 获取应用详情页面
     *
     * @return
     */
    public static Intent getAppDetailSettingIntent(Activity activity) {
        Intent localIntent = new Intent();
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", activity.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", activity.getPackageName());
        }
        return localIntent;
    }

    public static void openAppDetailSetting(Activity activity, int requstCode) {
        activity.startActivityForResult(getAppDetailSettingIntent(activity), requstCode);
        isAppSettingOpen = true;
    }

    /**
     * 开启白名单
     *
     * @param activity
     */
    public static void PowerService(Activity activity, int requstCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PowerManager pm = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
            if (!pm.isIgnoringBatteryOptimizations(activity.getPackageName())) {
                Intent intent = new Intent();
                String packageName = activity.getPackageName();
                intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + packageName));
                activity.startActivityForResult(intent, requstCode);
            }
        }
    }

    /**
     * 是否打开了电池优化权限
     * @param activity
     * @return
     */
    public static boolean isPowerService(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PowerManager pm = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
            if (pm.isIgnoringBatteryOptimizations(activity.getPackageName())) {
                return true;
            }
        }
        return false;
    }
}

